import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

import javax.swing.JPanel;

/**
 * 
 */

/**
 * @author Joe
 *
 */
public class Dice extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Image diceImage1;
	Image diceImage2;
	
	static final int MAX_VALUE = 6;
	
	// dice number in array
	int index;
	// value 1-6
	int value;
	// the dice position on the screen
	private int x;
	private int y;
	// has the player frozen this dice
	Boolean frozenDice;
	//random generator for dice rolling
	Random generator = new Random();
	
	public Dice()
	{
		index = value = x = y = 0;
		frozenDice = false;
		
		diceImage1 = getToolkit().getImage("dice.png");
		diceImage2 = getToolkit().getImage("dice2.png");
	}
	
	void Roll()
	{
		value = generator.nextInt(MAX_VALUE);
		index = value--;	
	}
	
	public void paintComponent(Graphics g)
	{
		int cellW, cellH;
		cellW = cellH = diceImage1.getHeight(this);
		if(frozenDice){
			g.drawImage(diceImage2, x, y, (x + cellW), y+cellH, index * cellW, 0, (index * cellW) + cellW, cellH, this);
		}
		else{
			g.drawImage(diceImage1, x, y, (x + cellW), y+cellH, index * cellW, 0, (index * cellW) + cellW, cellH, this);
		}
	}
	
	void setIndex(int a_index)
	{
		index = a_index;
	}
	
	void setPosition(int a_x, int a_y)
	{
		x = a_x;
		y = a_y;
	}
	
	void setFreeze(Boolean a_value) { frozenDice = a_value; }
	
	Boolean isFrozen() { return frozenDice; }
	int getDiceIndex() { return index; }
	int getDiceValue() { return value; }
	int getDimension() { return diceImage1.getHeight(this); }

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

}
